package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {
    // 登录员工 =>会话域的key 通过key可以拿到登录员工对象
    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    // 员工的权限 =>会话域的key
    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";

    //  获取请求对象
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes =
                ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
        return requestAttributes.getRequest();
    }

    // 获取session对象
    public static HttpSession getSession() {
        // 从请求对象获取session对象
        return getRequest().getSession();
    }

    // 登录成功,  往 session中存 员工对象
    public static void setEmployee(Employee employee) {
        getSession().setAttribute(EMPLOYEE_IN_SESSION,employee);
    }

    // 从会话域 session中 获取登录员工对象
    public static Employee getEmployee() {
        return ((Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION));
    }

    // 往 session中存 权限对象
    public static void setExpressions(List<String> expressions) {
        getSession().setAttribute(EXPRESSION_IN_SESSION,expressions);
    }

    // 从 session 中取 权限对象
    public static List<String> getExpressions() {
        return ((List<String>) getSession().getAttribute(EXPRESSION_IN_SESSION));
    }
}
